<?php
namespace app\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class AuthCheck implements MiddlewareInterface
{
    public function process(Request $request, callable $next) : Response
    {
        //权限验证
        $controller = $request->controller;
        $action = $request->action;

        //放过验证控制器
        $letgoAry = [
            [
                'controller' => 'app\controller\Index',
                'action' => [
                    'index',
                    'login',
                    'logout',
                ]
            ]
        ];
        if(!empty($letgoAry)){
            foreach($letgoAry as $k=>$vo){
                if($controller == $vo['controller'] && in_array($action,$vo['action'])){
                    //print_r('[放过验证控制器+方法。'.$controller.'-'.$action.']');echo "\n";
                    return $next($request);
                }
            }
        }

        //验证session
        $session = $request->session();
        if (!$session->get('token')) {
            return redirect('/');
        }

        return $next($request);
    }
}
